package cn.iocoder.yudao.module.im.controller.app.message;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessagePageReqVO;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessageReadReqVO;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessageRespVO;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessageSendReqVO;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessageSendRespVO;
import cn.iocoder.yudao.module.im.controller.app.message.vo.AppMessageUndoReqVO;
import cn.iocoder.yudao.module.im.service.message.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户APP - IM消息
 *
 * @author 芋道源码
 */
@Tag(name = "用户APP - IM消息")
@RestController
@RequestMapping("/member/message")
@Validated
@Slf4j
public class AppImMessageController {

    @Resource
    private MessageService messageService;

    @PostMapping("/send")
    @Operation(summary = "发送消息")
    public CommonResult<AppMessageSendRespVO> sendMessage(@Valid @RequestBody AppMessageSendReqVO sendReqVO) {
        Long userId = getLoginUserId();
        AppMessageSendRespVO result = messageService.sendMessage(userId, sendReqVO);
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获取消息分页列表")
    public CommonResult<PageResult<AppMessageRespVO>> getMessageList(@Valid AppMessagePageReqVO pageReqVO) {
        Long userId = getLoginUserId();
        PageResult<AppMessageRespVO> result = messageService.getMessagePage(userId, pageReqVO);
        return success(result);
    }

    @PostMapping("/read")
    @Operation(summary = "设置消息已读")
    public CommonResult<Boolean> readMessage(@Valid @RequestBody AppMessageReadReqVO readReqVO) {
        Long userId = getLoginUserId();
        messageService.readMessage(userId, readReqVO);
        return success(true);
    }

    @PostMapping("/undo")
    @Operation(summary = "撤回消息")
    public CommonResult<AppMessageRespVO> undoMessage(@Valid @RequestBody AppMessageUndoReqVO undoReqVO) {
        Long userId = getLoginUserId();
        AppMessageRespVO result = messageService.undoMessage(userId, undoReqVO);
        return success(result);
    }

}